<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace User\Controller;
use Think\Controller;

class ForgetController extends Controller {
    //put your code here
    
    public function index(){
        if(IS_POST){
            $username = I('username');
            $captcha = I('captcha');
            $re = check_verify($captcha);
            if($re){
                $map['user_mobile'] = $username;
                $maps['user_email'] = $username;
                $uType = M('users')->where($map)->find();
                $uTypes = M('users')->where($maps)->find();
                if($uType){
                    session('find_username',$username);
                    session('find_username_type',0);
                    $this->success('进入下一步',U('Forget/validation'));
                }
                if($uTypes){
                    session('find_username',$username);
                    session('find_username_type',1);
                    $this->success('进入下一步',U('Forget/validation'));
                }else{
                    $this->error('用户不存在！');
                }
            }else{
                $this->error('验证码不正确！');
            }
        }
        $this->display();
    }
    
   
    public function validation(){
        $find_username = session('find_username');
        $find_username_type = session('find_username_type');
        if(IS_POST){
            $captcha = I('captcha');
            $captcha_code = I('captcha_code');
            $session_sms = session('mobile_sms');
            $re = check_verify($captcha);
            if($re){
                if($captcha_code == $session_sms){
                    $this->success('进入下一步',U('Forget/setpasswd'));
                }else{
                    $this->error('手机验证码不正确！');
                }
            }else{
                $this->error('图片验证码不正确！');
            }
        }
        if(empty($find_username) || empty($find_username_type)){
            $this->redirect('Forget/index');
        }
        $this->display();
    }
    
    
    public function setpasswd(){
        $find_username = session('find_username');
        $find_username_type = session('find_username_type');
        if(empty($find_username) || empty($find_username_type)){
            $this->redirect('Forget/index');
        }
        if(IS_POST){
            $user_new_password = I('user_new_password');
            $user_confirm_password = I('user_confirm_password');
            if($user_confirm_password == $user_new_password){
                list($password, $salt) = password($user_confirm_password);
                $dates['password'] = $password;
                $dates['salt'] = $salt;
                if(session() == 0){
                    $where['user_mobile'] = session('find_username');
                }else{
                    $where['user_email'] = session('find_username');
                }
                $re = M('users')->where($where)->save($dates);
                if ($re !== FALSE) {
                    session('[destroy]');
                    $this->success('修改成功！',U('Login/index'));
                } else {
                    $this->error('修改失败！');
                }
            }else{
                $this->error('两次输入的密码不一样！');
            }
        }
        $this->display();
    }
}
